package com.hualidianping.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hualidianping.dto.LoginFormDTO;
import com.hualidianping.dto.Result;
import com.hualidianping.dto.UserDTO;
import com.hualidianping.entity.User;
import com.hualidianping.entity.UserInfo;
import com.hualidianping.service.IUserInfoService;
import com.hualidianping.service.IUserService;
import com.hualidianping.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static com.hualidianping.utils.RedisConstants.LOGIN_USER_KEY;

/**
 * 用户controller
 */
@Api(tags = "用户controller")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;
    @Resource
    private IUserInfoService userInfoService;
    /**
     * 发送手机验证码
     */
    @ApiOperation(value = "发送验证码")
    @PostMapping("code")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session){
        return userService.sendCode(phone,session);
    }
    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
        return userService.login(loginForm,session);
    }
    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        // TODO 实现登出功能
        String token = request.getHeader("authorization");
        stringRedisTemplate.delete(LOGIN_USER_KEY+token);
        return Result.ok();
//        return Result.fail("功能未完成");
    }

    @GetMapping("/me")
    public Result me(){
        //获取当前登录的用户并返
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId){
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }

    //根据id查询用户信息
    @GetMapping("/{id}")
    public Result queryUserById(@PathVariable("id") Long userId){
        // 查询详情
        User user = userService.getById(userId);
        if (user == null) {
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        // 返回
        return Result.ok(userDTO);
    }

    @PostMapping("/sign")
    public Result sign(){
        return userService.sign();
    }

    @GetMapping("/sign/count")
    public Result signCount(){
        return userService.signCount();
    }
}
